#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>

using namespace std;
typedef unsigned int uint;

const size_t DST_LEVELS = 6;
const size_t BASE_STIDE_LEVEL = 11;

struct DstNode
{
    DstNode* left;
    DstNode* right;
    void _deleteNode(DstNode* r)
    {
        if(r->left) delete r->left;
        if(r->right) delete r->right;
    }
    DstNode(DstNode* l, DstNode* r)
    {
        left = l; 
        right = r;
    }
    DstNode()
    {
        left = nullptr;
        right = nullptr;
    }
    ~DstNode()
    {
        _deleteNode(this);
    }
};

// add data
// prefix search
// delete and release memroy
class DigitalSearchTree
{
private:
    DstNode root;
public:
    DigitalSearchTree() : root(nullptr, nullptr)
    {
    }

    void insert(uint val)
    {
        DstNode *cur = &root;
        uint start = 0x1 << 15;
        while (start > 0)
        {
            if(start & val)
            {
                if(cur->right == nullptr)
                {
                    cur->right = new DstNode(nullptr, nullptr);
                }
                cur = cur->right;
            }
            else
            {
                if(cur->left == nullptr)
                {
                    cur->left = new DstNode(nullptr, nullptr);
                }
                cur = cur->left;
            }
            start = start >> 1;
        }
    }
    
    bool contains(uint val)
    {
        DstNode *cur = &root;
        uint start = 0x1 << 15;
        while (start > 0)
        {
            if(start & val)
            {
                if(cur->right == nullptr)
                {
                    return false;
                }
                cur = cur->right;
            }
            else
            {
                if(cur->left == nullptr)
                {
                    return false;
                }
                cur = cur->left;
            }
            start = start >> 1;
        }
        return true;
    }

uint GetMaxXor(uint val)
{
    DstNode *cur = &root;
    uint start = 0x1 << 15;
    uint collector = 0;
    while (start > 0)
    {
        if((cur->right != nullptr) && (!(start & val)))
        {
            collector = collector | start;
            cur = cur->right;
        }
        else if((cur->left != nullptr) && (start & val))
        {
            cur = cur->left;
            collector = collector | start;
        }
        else
        {
            if(cur->left != nullptr)
            {
                cur = cur->left;
            }
            else
            {
                 cur = cur->right;
            }
        }
        start = start >> 1;
    }
    return collector;
}
};


void test_dst()
{
    DigitalSearchTree dst;
    uint a = 61224;
    uint b = 2324;
    uint c = 3945;
    uint maxa = 0;
    uint maxb = 0;
    uint maxc = 0;
    for(uint i = 0; i < 65000; i = i + 100)
    {
        dst.insert(i);
        if((a ^ i) > maxa) maxa = (a ^ i);
        if((b ^ i) > maxb) maxb = (b ^ i);
        if((c ^ i) > maxc) maxc = (c ^ i);

    }

    for(uint i = 0; i < 65000; i = i + 100)
    {
        if(!dst.contains(i)) cout << "not found: " << i << endl;
    }

    for(uint i = 0; i < 65000; i++)
    {
        if(i % 100 != 0 && dst.contains(i)) cout << "phantom found: " << i << endl;
    }
    if(maxa != dst.GetMaxXor(a)) cout << "wrong with maxa" << endl;
    if(maxb != dst.GetMaxXor(b)) cout << "wrong with maxb" << endl;
    if(maxc != dst.GetMaxXor(c)) cout << "wrong with maxc" << endl;
    cout << "test done" << endl;
}

size_t power(size_t x, size_t y)
{
    if(y <= 1) return  y == 0 ? 1 : x;
    if((y & 0x1) == 0)
    {
        return power(x*x, y/2);
    }
    else
    {
        return power(x*x, (y - 1) / 2) * x;
    }
}




int main() {

   //  test_dst();

    size_t testCases = 0;
    cin >> testCases;
    for(size_t counter = 0; counter < testCases; counter++)
    {
        size_t numCount = 0;
        size_t testItemCount = 0;
        cin >> numCount;
        cin >> testItemCount;
        vector<uint> vNums;
        for(size_t i = 0; i < numCount; i++)
        {
            uint x = 0;
            cin >> x;
            vNums.push_back(x);
        }
        vNums.shrink_to_fit();
        vector<vector<DigitalSearchTree*>>  dsts;
        for(size_t i = 0; i <= DST_LEVELS; i++)
        {
            dsts.emplace_back(vector<DigitalSearchTree*>());
        }
        for(size_t x = 0; x < numCount; x++)
        {
            uint current = vNums[x];
            for(size_t i = 0; i <= DST_LEVELS; i++)
            {
                size_t stride = power(2, i + BASE_STIDE_LEVEL);
                size_t xthStride = x / stride;
                if((x % stride) == 0)
                {
                    dsts[i].push_back(new DigitalSearchTree());
                }
                dsts[i][xthStride]->insert(current);
            }
        }
        // handle each test case
        for(size_t i = 0; i < testItemCount; i++)
        {
            size_t a = 0;
            size_t p = 0;
            size_t q = 0;
            cin >> a;
            cin >> p;
            cin >> q;

            //// brute force
            //uint bfmax_xor = 0;
            //for(size_t it = p-1; it < q; it++)
            //{
            //    if((a ^ vNums[it]) > bfmax_xor)
            //    {
            //        bfmax_xor = a ^ vNums[it];
            //    }
            //}
            //// brute force end

            if((q - p) < power(2, BASE_STIDE_LEVEL)) // brute force for small ones
            {
                uint max_xor = 0;
                for(size_t it = p-1; it < q; it++)
                {
                    auto tmp = a ^ vNums[it];
                    max_xor = tmp > max_xor ? tmp : max_xor;
                }
                cout << max_xor << endl;
            }
            else // Initially, let's just use the finest level strides to calculate it
                 // we will optimize later if this does not pass the time limit
            {
                uint max_xor = 0;
                size_t stride = power(2, BASE_STIDE_LEVEL);
                size_t xthStride = 0;
                if((p % stride) == 0)
                {
                    xthStride = (p / stride);
                }
                else
                {
                    xthStride = (p / stride) + 1;
                }
                size_t strideStart = xthStride * stride;
                size_t end = min(strideStart, q);
                // cout << "from " << p - 1 << " to " << end << endl;
                for(size_t it = p - 1; it < end; it++)
                {
                    auto tmp = a ^ vNums[it];
                    max_xor = tmp > max_xor ? tmp : max_xor;
                }
                size_t lastPos = end;
                size_t stride_level = 0;
                while(strideStart + stride < q)
                {
                    size_t two_stride = stride + stride;
                    while((strideStart % two_stride == 0))
                    {
                        if((strideStart + two_stride) < q)
                        {
                            stride = two_stride;
                            ++stride_level;
                            // cout << "stride_level" << stride_level << endl;
                            xthStride = strideStart / two_stride;
                            two_stride = stride + stride;
                        }
                        else
                        {
                            break;
                        }
                        
                    }
                    // cout << "sride: " << xthStride << endl;
                    auto tmp = dsts[stride_level][xthStride]->GetMaxXor(a);
                    max_xor = tmp > max_xor? tmp : max_xor;
                    strideStart += stride;
                    lastPos = strideStart;
                    ++xthStride;

                    while((strideStart + stride >= q) && (stride_level > 0))
                    {
                        stride = stride / 2;
                        --stride_level;
                        // cout << "stride_level" << stride_level << endl;
                        xthStride = strideStart / stride;
                    }
                }
                // cout << "from " << lastPos << " to " << q << endl;
                for(size_t it = lastPos; it < q; it++)
                {
                    auto tmp = a ^ vNums[it];
                    max_xor = tmp > max_xor ? tmp : max_xor;
                }
                cout << max_xor << "\n";
                // if(bfmax_xor != max_xor) cout << "wrong answer" << endl;
            }
        }

        for(size_t i = 0; i <= DST_LEVELS; i++)
        {
            for(auto it = dsts[i].begin(); it != dsts[i].end(); it++)
            {
                delete *it;
            }
        }
    }

    //size_t stop;
    //cin >> stop;
    return 0;
}
